package cn.vconinfo.database.outer.source;

import cn.vconinfo.database.handler.DynamicDataSource;
import cn.vconinfo.database.interceptor.MybatisInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

/**
 * @author vconinfo
 * @createdOn 2018/4/30
 * @description:
 */
@Configuration
@ConditionalOnClass(value = DynamicDataSourceRegister.class)
@ConditionalOnProperty(name="spring.config.profile", havingValue = "false", matchIfMissing = false)
public class DefaultDataSourceConfig {
    protected static Logger logger = LoggerFactory.getLogger(DefaultDataSourceConfig.class);

    @Autowired
    @Qualifier("dataSource")
    DynamicDataSource writeDataSource;

//    @Bean(name = "defaultTransactionManager")
//    @Primary
//    public DataSourceTransactionManager rdsTransactionManager() {
//        return new DataSourceTransactionManager(writeDataSource);
//    }

//    @Bean(name = "defaultSqlSessionFactory")
//    @Primary
//    public SqlSessionFactory writeSqlSessionFactory(@Qualifier("dataSource") DynamicDataSource writeDataSource) throws Exception {
//        final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
//        sessionFactory.setDataSource(writeDataSource);
//        // 添加mybatis拦截器
//        sessionFactory.setPlugins(new Interceptor[]{new MybatisInterceptor()});
//        sessionFactory.setMapperLocations(
//                new PathMatchingResourcePatternResolver()
//                        .getResources("classpath*:mapper/**/*.xml")
//        );
//        //开启驼峰命名转换
//        sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
//        //开启call-setters-on-nulls属性
//        sessionFactory.getObject().getConfiguration().setCallSettersOnNulls(true);
//        //当开启这个设置时,返回行的所有列都是空时,MyBatis会返回一个空实例
//        sessionFactory.getObject().getConfiguration().setReturnInstanceForEmptyRow(true);
//        return sessionFactory.getObject();
//    }


//    /**
//     *  由于上面重定义了sessionFactory导致mybatis的配置没有生效
//     *
//     * @return
//     */
//    @Bean
//    @ConfigurationProperties(prefix = "mybatis.configuration")
//    public org.apache.ibatis.session.Configuration globalConfiguration(){
//        return new org.apache.ibatis.session.Configuration();
//    }

}